import scrapy
from demo2.items import wereadItem
# scrapy crawl weread -o weread.csv

class WereadSpider(scrapy.Spider):
    name = "weread"
    allowed_domains = ["weread.qq.com"]
    start_urls = ["https://weread.qq.com/web/category"]

    def parse(self, response):
        list_items= response.css("#routerView > div.ranking_page_content > div.ranking_page_content_list_container > ul > li")
        for selector in list_items:
            item = wereadItem()

            item['rank'] = selector.css('.wr_bookList_item_index::text').get().strip()
            item['book_name'] = selector.css('.wr_bookList_item_title::text').get().strip()
            item['author'] = selector.css('.wr_bookList_item_author a::text').get().strip()
            # item['book_cover'] = selector.css('.wr_bookCover_img::attr(src)').get()
            item['today_read_count'] = selector.css('.wr_bookList_item_reading_number::text').get().strip()
            item['recommend_percent'] = selector.css('.wr_bookList_item_reading_percent::text').get().strip()
            book_description = selector.css('.wr_bookList_item_desc::text').getall()
            item['book_description'] = ''.join([line.strip() for line in book_description if line.strip()])
            # item['book_url'] = selector.css('.wr_bookList_item_link::attr(href)').get()

            yield item


